package org.base23.uaa.business.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.base23.uaa.business.service.MenuPermissionService;
import org.base23.uaa.business.service.RoleService;
import org.base23.uaa.business.service.UserRoleService;
import org.base23.uaa.core.domain.dto.AddRoleParam;
import org.base23.uaa.core.domain.dto.CurrentUser;
import org.base23.uaa.core.domain.dto.RoleBindUserParam;
import org.base23.uaa.core.domain.dto.RoleQueryBuilder;
import org.base23.uaa.core.domain.dto.UpdateRoleParam;
import org.base23.uaa.core.exception.UaaExceptions;
import org.base23.uaa.core.util.LoginSessionTool;
import org.base23.web.model.Result;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/role")
@Validated
@Tag(name = "角色管理", description = "角色CRUD、权限分配及用户关联")
public class RoleController {

  private final MenuPermissionService menuService;

  private final RoleService roleService;

  private final UserRoleService userRoleService;

  public RoleController(MenuPermissionService menuService, RoleService roleService,
      UserRoleService userRoleService) {
    this.menuService = menuService;
    this.roleService = roleService;
    this.userRoleService = userRoleService;
  }

  // 获取当前用户（所选择的角色）拥有的权限信息
  @GetMapping("/permissions/tree")
  @Operation(summary = "获取当前角色权限树", description = "获取当前登录用户所选角色的权限树状结构")
  public Result getRolePermissionTree() {
    CurrentUser currentUser = LoginSessionTool.getCurrentUser();
    return Result.data(menuService.getRolePermissionTree(currentUser.getRoleCode()));
  }

  @PostMapping
  @Operation(summary = "添加角色", description = "创建新的角色")
  public Result addRole(@Validated @RequestBody AddRoleParam param) {
    return Result.data(roleService.addRole(param));
  }

  @GetMapping("/page")
  @Operation(summary = "分页查询角色", description = "支持条件筛选的角色分页查询")
  public Result pageQueryRoles(RoleQueryBuilder param) {
    return Result.data(roleService.pageQueryRoles(param));
  }

  @GetMapping
  @Operation(summary = "查询所有角色", description = "获取所有角色列表")
  public Result findAllRoles() {
    return Result.data(roleService.findAllRoles());
  }

  @DeleteMapping("/{id}")
  @Operation(summary = "删除角色", description = "注意：当前系统禁止删除角色")
  public Result delete(@Parameter(description = "角色ID") @PathVariable("id") int id) {
    throw UaaExceptions.FORBID_DELETE.exception();
  }

  @PatchMapping("/{id}")
  @Operation(summary = "更新角色", description = "修改角色信息")
  public Result updateRole(
      @Parameter(description = "角色ID") @PathVariable("id") int id, 
      @RequestBody UpdateRoleParam param) {
    return Result.row(roleService.updateRole(param));
  }

  @PatchMapping("/users/remove/{roleId}")
  @Operation(summary = "角色解绑用户", description = "从角色中移除指定用户")
  public Result roleUnBindUsers(
      @Parameter(description = "角色ID") @PathVariable("roleId") int roleId,
      @Validated @RequestBody RoleBindUserParam param) {
    param.setRoleId(roleId);
    userRoleService.roleUnBindUsers(param);
    return Result.data(true);
  }

  @PatchMapping("/users/add/{roleId}")
  @Operation(summary = "角色绑定用户", description = "为角色添加指定用户")
  public Result roleBindUsers(
      @Parameter(description = "角色ID") @PathVariable("roleId") int roleId,
      @Validated @RequestBody RoleBindUserParam param) {
    param.setRoleId(roleId);
    userRoleService.roleBindUsers(param);
    return Result.data(true);
  }
}
